<template>
	<view>
		<j-cell-group>
			<j-cell title="设备名称" :tips="deviceName" @jftap="openPopup"></j-cell>
			<!-- <j-cell title="全双工语音对讲" sub-title="打开后,按下对讲按钮,双方可同时语音讲话" :more="false">
				<view slot="tips" style="margin-right:20rpx;">
					<switch></switch>
				</view>
			</j-cell> -->
			<!-- <j-cell title="设备自动休眠时间" subTitle="时间越长,设备的续航时间就会越短" :tips="sleepTime"
				:to="`/jvss/pagesDevice/device/device-live/setting/selectItem?type=2&token=${token}`"></j-cell>
			<j-cell title="设备语言" :tips="language"
				:to="`/jvss/pagesDevice/device/device-live/setting/selectItem?type=1&token=${token}`"></j-cell> -->
			<j-cell ref="jcell" title="对讲喇叭音量" :more="false">
				<view slot="tips" class="laba">
					<slider style="width: 100%;" :min="1" :value="volume" @change="sliderChange" step="1" show-value />
				</view>
			</j-cell>
		</j-cell-group>
		<uni-popup ref="inputDialog" class="popupName" type="dialog" :is-mask-click="false" @maskClick="maskClick">
			<view class="form">
				<view class="u-f-ajc title">修改名称</view>
				<view class="form-item">
					<uni-easyinput v-model="editDeviceName" placeholder="请输入设备名称"></uni-easyinput>
				</view>
				<view class="u-f-r form-item">
					<view class="u-f-ajc cancel" @click="close">取消</view>
					<view class="u-f-ajc conf" @click="submit">保存</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	const { isEmojiCharacter } = require('@/utils/util.js');
	export default {
		data() {
			return {
				device:{},
				token: '',
				deviceNo: '',
				deviceName: '',
				editDeviceName: '',
				language: '',
				sleepTime: '',
				volume: 0,
				volumeQuery: {}, //喇叭音量
				lanQuery: {} //设备语言
			}
		},
		onLoad(opt) {
			let enData = decodeURIComponent(opt.device)
			let options = JSON.parse(enData);
			this.device = options
			this.token = options.token;
			this.editDeviceName = options.deviceName;
			this.deviceNo = options.deviceNo;
			let deviceList = uni.getStorageSync('deviceList');
			if(deviceList.length>0){
				for (let i = 0; i < deviceList.length; i++) {
					const item = deviceList[i];
					if (item.deviceNo === this.deviceNo) {
						this.deviceName = item.deviceName;
						break;
					}
				}
			}else{
				this.deviceName = this.device.deviceName
			}
		},
		onShow() {
			// this.getDeviceLanguage()
			// this.getSleepTime()
			let status = this.device.status
			if(status.status === 'online' ){
				if(status.wakeUpStatus === '1' || (status.wakeUpStatus === '0' && status.wakeUpEnable === '1')){
					this.getFVideoVolumeParam()
				}else if(!(status.wakeUpStatus === '0' && status.wakeUpEnable === '0')){
					let cacheData = uni.getStorageSync(this.deviceNo)
					if(!cacheData.volume){
						this.getFVideoVolumeParam()
					}
				}
			}
			this.getCacheData()
			this.$logs({info: '基本设置页',sn:this.deviceNo,status:status,cacheData:uni.getStorageSync(this.deviceNo)})
		},
		methods: {
			maskClick() {
                this.$refs.inputDialog.close()
                this.editDeviceName = ''
            },
			// 缓存数据
			setCacheData(param,value){
				let cacheData = uni.getStorageSync(this.deviceNo)
				if(cacheData){
					// cacheData['Volume'][param] = value
					cacheData[param] = value
					uni.setStorageSync(this.deviceNo,cacheData)
					console.log('缓存===2',cacheData);
				}else{
					// let Volume = {}
					// Volume[param] = value
					uni.setStorageSync(this.deviceNo,{volume:value})
				}
			},
			//获取缓存数据
			getCacheData(){
				let cacheData = uni.getStorageSync(this.deviceNo)
				if(cacheData){
					if(cacheData.volume){
						this.volume = cacheData.volume
					}
				}
			},
			// 获取设备状态
			getDeviceStatus(type){
				const data = {
					token: [this.token]
				};
				getApp().globalData.api.getDeviceStatus(data, (result) => {
					let item = result[0]
					if(item.status === 'online' ){
						if(item.wakeUpStatus === '0' ){
							if(item.wakeUpEnable === '0' ){
								//深度休眠 手动唤醒
								uni.showToast({
									title:'设备处于深度休眠，请手动唤醒',
									icon:'none'
								})
							}else{
								//浅休眠 可以唤醒
								this.setConfig(this.volumeQuery)
							}
						}else if(item.wakeUpStatus === '1'){
							//已唤醒 在线
							this.setConfig(this.volumeQuery)
						}else if(item.wakeUpStatus === '2'){
							// 准备休眠中
							uni.showToast({
								title:'设备休眠中，请稍后唤醒',
								icon:'none'
							})
						}
					}else{
						//离线
						uni.showToast({
							title:'设备已离线',
							icon:'none'
						})
					}
				})
			},
			//获取喇叭音量fVideo.Volume参数
			getFVideoVolumeParam() {
				let data = {
					Name: 'fVideo.Volume'
				}
				this.getConfig(data)
			},
			//获取设备语言
			getDeviceLanguage() {
				let data = {
					Name: 'General.Location'
				}
				this.getConfig(data)
			},
			//获取当前睡眠时间
			getSleepTime() {
				let data = {
					Name: 'System.ManageShutDown'
				}
				this.getConfig(data)
			},
			// 设置配置
			setconfig() {
				uni.showLoading();
				let token = this.token;
				let data = {
					Name: 'Detect.MotionDetect',
					'Detect.MotionDetect': this.DetectMotionDetectList
				};
				getApp().globalData.api.setconfig(data, token, (result) => {
					uni.showToast({
						title: '保存成功'
					});
				});
			},
			//设置参数
			setConfig(data) {
				uni.showLoading();
				let token = this.token;
				getApp().globalData.api.setconfig(data, token, (result) => {
					console.log('设置参数：', result);
					uni.hideLoading();
					setTimeout(() => {
						uni.showToast({
							title: '设置成功',
							icon: 'none'
						});
					}, 100)
					if(result.Name == 'fVideo.Volume'){
						this.setCacheData('volume',this.volume)
					}
				})
			},
			// 设置喇叭音量大小
			sliderChange(e) {
				this.volume = e.detail.value
				this.volumeQuery['fVideo.Volume'][0].LeftVolume = this.volume
				this.volumeQuery['fVideo.Volume'][0].RightVolume = this.volume
				setTimeout(() => {
					// this.setConfig(this.volumeQuery)
					this.getDeviceStatus()
				}, 500)
				console.log('slider', e);
			},
			// 获取参数
			getConfig(data) {
				uni.showLoading({
					mask:true
				});
				let token = this.token;
				getApp().globalData.api.getconfig(data, token, (result) => {
					uni.hideLoading();
					console.log('获取参数：', result);
					// if (result.code == 2000) {
						//音量
						if (result.Name == 'fVideo.Volume') {
							this.volumeQuery = result
							this.volume = result['fVideo.Volume'][0].LeftVolume
							console.log('音量缓存===');
							this.setCacheData('volume',this.volume)
						}
						//设备语言
						if (result.Name == 'General.Location') {
							this.lanQuery = result
							this.language = result['General.Location'].Language == "SimpChinese" ? '中文' : "英文"
						}
						//睡眠时间
						if (result.Name == 'System.ManageShutDown') {
							let time = result['System.ManageShutDown'].ShutDownMode
							if (time == -2) {
								this.sleepTime = '永不休眠'
							} else {
								this.sleepTime = time + '秒'
							}
							console.log("this.sleepTime: ", this.sleepTime);
							console.log("this.sleepTime22: ", time == -2);
						}
					// } else {
					// 	uni.showToast({
					// 		title: result.msg,
					// 		icon: 'none'
					// 	})
					// }
				})
			},
			// 修改设备名称
			submit() {
				if (this.editDeviceName.replace(/\s/g, '') === '') {
					uni.showToast({
						title: '请输入设备名称'
					});
					return;
				}
				if(this.editDeviceName.length>32 || this.editDeviceName.length<2){
					uni.showToast({
						title: '设备名称长度2~32位',
						icon:'none'
					});
					return;
				}
				if(isEmojiCharacter(this.editDeviceName)){
					uni.showToast({
						title: '设备名称不能为表情包',
						icon:'none'
					});
					return;
				}
				let data = {
					deviceNo: this.deviceNo,
					deviceName: this.editDeviceName
				};
				getApp().globalData.api.updateDevice(data, (result) => {
					uni.showToast({
						title: '修改成功'
					})
					this.deviceName = this.editDeviceName
					this.$refs.inputDialog.close()
					this.focus = false
					let deviceList = uni.getStorageSync('deviceList');
					for (let i = 0; i < deviceList.length; i++) {
						const item = deviceList[i];
						if (item.deviceNo === this.deviceNo) {
							item.deviceName = this.deviceName;
							break;
						}
					}
					uni.setStorageSync('deviceList', deviceList);
					// 更新设备信息
					uni.$emit('editDevice',{data:deviceList})
					let jsonData = {
						sn:this.device.deviceNo,
						deviceName:this.editDeviceName
					}
					uni.$emit('editDeviceName',jsonData)
				});
			},
			tapEdit(ev) {
				console.log(ev);
				this.$refs.jslider.onBindHide();
			},
			// 显示修改设备名称弹框
			openPopup() {
				this.editDeviceName = this.deviceName
				this.$refs.inputDialog.open()
			},
			//关闭弹框
			close() {
				this.$refs.inputDialog.close()
				this.deviceName = this.deviceName
			},
			// 自定义隐藏按钮事件
			onBintapEdit(ev) {
				console.log('自定义隐藏按钮事件：', ev);
				this.$refs.jcell.onBindHide();
			}
		}
	}
</script>

<style lang="scss">
	.popupName {
		/deep/ .uni-popup__wrapper {
			top: -100rpx;
		}
	}
	.form {
		background: white;
		padding: 60rpx;
		border-radius: 10rpx;

		.title {
			font-size: 34rpx;
			font-weight: bold;
			line-height: 49rpx;
			color: #464646;
		}

		.form-item {
			margin-top: 40rpx;

			// input{
			// 	border: 1rpx solid #A5A5A5;
			// 	border-radius: 8rpx;
			// 	padding: 10rpx;
			// }
			.cancel {
				background: #F6F7FB;
				color: #000000;
				font-size: 30rpx;
				width: 237rpx;
				height: 84rpx;
				border-radius: 42rpx;
			}

			.conf {
				margin-left: 20rpx;
				background: #FF0025;
				color: #ffffff;
				font-size: 30rpx;
				width: 237rpx;
				height: 84rpx;
				border-radius: 42rpx;
			}
		}

		.form-title {
			text-align: center;
			margin-bottom: 40rpx;
			font-weight: bold;
		}
	}

	.actionFlex {}

	.timeselect {
		display: flex;
		justify-content: space-between;
		padding: 10rpx;
		font-size: 26rpx;
		border-bottom: #eee 1rpx solid;
		height: 50rpx;
	}

	.laba {
		width: calc(50vw - 50rpx);
		display: flex;
		justify-content: end;
		margin-right: 20rpx;
	}
</style>
